package core;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import superweapons.SuperWeapon;
import towers.Bunker;
import towers.Cannon;
import towers.MissileLauncher;
import towers.NuclearBase;
import towers.Plasmagun;

/**
 * @author Arno de Jager
 *
 */
public class Controller implements ActionListener {
	
	private View view;
	private Model model;
	private Mouse mouse;
	private ArrayList<SuperweaponController> superweapons;
	
	public static Controller main;
	
	// Gebruikt voor mapeditor
	private String mapeditor_type = "base";
	
	public Controller(Model m)
	{
		this.model = m;
		main = this;
		this.mouse = new Mouse( m, this );
		
		superweapons = new ArrayList<SuperweaponController>();
		
		//Add superweapons!
		addSuperweapon("Blizzard",new ImageIcon(this.getClass().getResource("../images/button_blizzard.gif")),180000);
		addSuperweapon("Firestorm",new ImageIcon(this.getClass().getResource("../images/button_firestorm.gif")),150000);
		addSuperweapon("NukePlane",new ImageIcon(this.getClass().getResource("../images/button_nuke.gif")),240000);
		
		view = new View(m, this);
		model.addObserver( view );
	}
	
	public View getView() {
		return this.view;
	}
	
	public String getMapeditorType()
	{
		return mapeditor_type;
	}

	public void actionPerformed(ActionEvent e) {
		if( model.isMapEditor() )
		{
			if( e.getActionCommand().equals("road"))
			{
				mapeditor_type = e.getActionCommand();
			}
			else if ( e.getActionCommand().equals("base") )
			{
				mapeditor_type = e.getActionCommand();
			}
			else if( e.getActionCommand().equals("undo") )
			{
				mouse.undoAction();
			}
			else if( e.getActionCommand().equals("start") )
			{
				model.startGame();
			}
		}
		else
		{
			// Knoppen mogen alleen werken als het spel niet afgelopen is
			if ( !model.hasEnded() ) {
				// Bouw torens
				if ( e.getActionCommand().equals( "Cannon" )) {
					this.mouse.setSelectedItem( new Cannon( null ) );
				}
				else if ( e.getActionCommand().equals( "Bunker" )) {
					this.mouse.setSelectedItem( new Bunker( null ) );
				}
				else if ( e.getActionCommand().equals( "Missile Launcher" )) {
					this.mouse.setSelectedItem( new MissileLauncher( null ) );
				}
				else if ( e.getActionCommand().equals( "Nuclear Base" )) {
					this.mouse.setSelectedItem( new NuclearBase( null ) );
				}
				else if ( e.getActionCommand().equals( "Plasmagun" )) {
					this.mouse.setSelectedItem( new Plasmagun( null ) );
				}
				
				// Etcetera
				else if ( e.getActionCommand().equals( "Next Wave" )) {
					model.nextWave();
				}
				else if ( e.getActionCommand().equals( "Repair" )){
					model.repair(this.mouse.getSelectedItem());
				}
				else if ( e.getActionCommand().equals( "Upgrade" )){
					model.upgrade(this.mouse.getSelectedItem());
				}
				else if ( e.getActionCommand().equals( "Sell" )){
					model.sell(this.mouse.getSelectedItem());
				}
				else if ( e.getActionCommand().equals( "Abandon" )){
					model.abandon();
				}
				else if ( e.getActionCommand().equals( "Pause" )){
					model.togglePause();
				}
			}
			
			if ( e.getActionCommand().equals( "Restart" )){
				model.getLauncher().chooseDifficulty();
			}
		}
	}

	/**
	 * @return the mouse
	 */
	public Mouse getMouse() {
		return mouse;
	}
	
	public void addSuperweapon(String name, ImageIcon icon, int timer) {
		superweapons.add(new SuperweaponController(this, model, name,icon,timer));
	}
	
	public SuperweaponController getSuperweapon(String name) {
		for (SuperweaponController sc : superweapons) {
			if (sc.getName().equals(name)) {
				return sc;
			}
		}
		return null;
	}
	
	public ArrayList<SuperweaponController> getSuperweapons() {
		return superweapons;
	}
	
	public void activateSuperWeapon(SuperWeapon w) {
		mouse.setSelectedItem(w);
	}
}
